package com.wjx.happensbefore.finalizer;

public class FinalizerRule {
    private static boolean stop = false;

    public static void main(String[] args) {
        Test test = new Test();
        //test设置为null以后就可以回收了
        test = null;
        while (true) {
            //促使垃圾回收
            byte[] bytes = new byte[1024 * 1024];
        }
    }

    static class Test {
        public Test() {
            stop = true;
            System.out.println("set stop true in constructor");
        }

        @Override
        protected void finalize() throws Throwable {
            if (stop) {
                System.out.println("stop true in finalize, threadName  " + Thread.currentThread().getName());
            } else {
                System.out.println("stop false in finalize, threadName " + Thread.currentThread().getName());
            }
        }
    }
}
